package com.frog.service.friend.service.api;


import com.frog.service.friend.api.GroupFriendlyRelationShipsApi;
import com.frog.service.friend.enums.FriendListTypeEnum;
import com.frog.service.friend.model.PageFriendListRespModel;
import com.frog.service.friend.model.PageFriendReqModel;
import com.frog.service.friend.model.domain.FriendFollowingRelationshipDO;
import com.frog.service.friend.service.cache.FriendFollowService;
import com.frog.service.friend.service.factory.FriendListFactory;
import com.frog.service.friend.service.friendList.AbstractFriendListStrategy;
import com.frog.service.friend.utils.FriendRelationshipUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class GroupFriendlyRelationShipServiceImpl implements GroupFriendlyRelationShipsApi {

    @Autowired
    private FriendListFactory factory;
    @Autowired
    private FriendFollowService friendFollowService;


    /**
     * 根据查询类型获取用户的关系结果集
     * 1、{@code type == followed}            我关注的好友列表(包含我的关注、双向关注用户)
     * 2、{@code type == followedMe}          关注我的好友 (包含关注我的、双向关注用户)
     * 3、{@code type == follwedMutual}       我的双向关注好友
     * 4、{@code type == bestFriended}        我的bestfriend好友 (包含我的bestfriend关注、双向bestfriend关注用户)
     * 5、{@code type == bestFriendedMe}      bestfriend好友有我的 (包含bestfriend关注我的、双向bestfriend关注用户)
     * 6、{@code type == bestFriendedMutual}  我的双向bestfriend关注好友
     * 7、{@code type == blocked}             我拉黑的用户
     * 8、{@code type == blockedMe}           拉黑我的用户
     * 9、{@code type == blockedMutual}       双向拉黑的用户
     */
    @Override
    public PageFriendListRespModel<String> getUserRelationshipsByType(PageFriendReqModel pageFriendReqModel) {
        AbstractFriendListStrategy service = factory.getService(pageFriendReqModel.getType());
        return service.getFriendList(pageFriendReqModel);
    }


    /**
     * 根据查询类型获取用户的关系结果集,并返回好友之间的关系信息
     */
    @Override
    public PageFriendListRespModel<FriendFollowingRelationshipDO> getUserRelationshipsDoByType(PageFriendReqModel pageFriendReqModel) {
        // 根据好友类型查询friendUserId
        PageFriendListRespModel<String> friendList = getUserRelationshipsByType(pageFriendReqModel);
        String[] friendUserIds = friendList.getFindRespVoList().toArray(new String[0]);
        // 缓存查询好友之间详情信息
        Map<String, FriendFollowingRelationshipDO> relationshipMap = getRelationshipsBetweenUsers(String.valueOf(pageFriendReqModel.getUserId()), friendUserIds);
        // 排序,封装结果集
        List<FriendFollowingRelationshipDO> list = new ArrayList<>(friendUserIds.length);
        for (String fid : friendUserIds) {
            if (StringUtils.isBlank(fid)) continue;
            FriendFollowingRelationshipDO f = relationshipMap.get(fid);
            if (f == null) continue;
            list.add(f);
        }
        // 定义返回结果集
        PageFriendListRespModel<FriendFollowingRelationshipDO> resp = new PageFriendListRespModel<>();
        resp.setFindRespVoList(list);
        resp.setFindRespVoListSize(friendList.getFindRespVoListSize());
        resp.setToTalSize(friendList.getToTalSize());
        resp.setPageSet(friendList.getPageSet());
        return resp;
    }


    /**
     * 根据查询类型获取用户的关系结果集长度
     */
    @Override
    public long getUserRelationshipsCountByType(String userId, FriendListTypeEnum type) {
        if (StringUtils.isBlank(userId) || type == null) return 0;
        AbstractFriendListStrategy service = factory.getService(type);
        return service.total(userId);
    }


    /**
     * 获取一个用户和多个用户之间的关系信息
     */
    @Override
    public Map<String, FriendFollowingRelationshipDO> getRelationshipsBetweenUsers(String userId, String... friendUserId) {
        Map<String, FriendFollowingRelationshipDO> respMap = new HashMap<>();
        if (StringUtils.isBlank(userId) || friendUserId == null) return respMap;
        String[] ids = FriendRelationshipUtil.processRelationId(userId, friendUserId).toArray(new String[]{});
        List<FriendFollowingRelationshipDO> list = friendFollowService.select(ids);
        for (FriendFollowingRelationshipDO f : list) {
            if (f == null) continue;
            String fid = String.valueOf(f.getToUserId());
            if (StringUtils.isBlank(fid)) continue;
            respMap.put(fid, f);
        }
        return respMap;
    }

}